﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace CPPEI.Coolzon.Tools.INSVideoTool
{
    /// <summary>
    /// HideWindow.xaml 的交互逻辑
    /// </summary>
    public partial class HideWindow : Window
    {
        private Thread _thread;
        public HideWindow()
        {
            InitializeComponent();
        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);

            _thread = new Thread(running);
            _thread.Start();
        }

        private void running()
        {
            while (true)
            {
                string param = GetNextVideoParam();
                IntPtr closeParam = GetNextClosingVideo();
                if (!string.IsNullOrEmpty(param))
                {
                    RemoveVideoParam(param);
                    try
                    {
                        string[] args = param.Split(new char[] { ',' });

                        this.Dispatcher.Invoke(() =>
                        {
                            MainWindow mainWindow = new MainWindow(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
                            mainWindow.Owner = this;
                            mainWindow.LoadCompleted += mainWindow_LoadCompleted;
                            mainWindow.WindowStartupLocation = System.Windows.WindowStartupLocation.Manual;
                            mainWindow.Left = -100;
                            mainWindow.Show();
                        });
                    }
                    catch (Exception ex)
                    {
                        (Application.Current as App).WriteLog(Environment.NewLine + "错误1:" + ex.StackTrace + "\nParam:" + param);
                    }
                }
                else if (closeParam != IntPtr.Zero)
                {
                    RemoveClosingVideo(closeParam);
                    try
                    {
                        if ((Application.Current as App).videoWindow.ContainsKey(closeParam))
                        {
                            this.Dispatcher.Invoke(() =>
                            {
                                (Application.Current as App).videoWindow[closeParam].Close();
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                        (Application.Current as App).WriteLog(Environment.NewLine + "错误2:" + ex.StackTrace + "\nParam:" + closeParam);
                    }
                }
                else
                {
                    Thread.Sleep(100);
                }
            }
        }

        void mainWindow_LoadCompleted(Window arg1, string arg2, IntPtr arg3)
        {
            (Application.Current as App).videoWindow.Add(arg3, (MainWindow)arg1);
            (Application.Current as App)._pipeClientWriter.WriteLine(string.Format("opencamera:>{0},{1}", arg2, arg3));
        }

        protected override void OnClosed(EventArgs e)
        {
            if (_thread != null)
            {
                _thread.Abort();
                _thread = null;
            }
            base.OnClosed(e);
        }
    }
}
